<?php
/**
 * 
 * app 疯狂工厂 - 事件
 * @author will
 *
 */
class App_Factory_Event {
	/**
	 * 获取一个事件 (随机获取)
	 * @param <string> $place 地点
	 */
	static public function get($place) {
		$Db = loadDB();
		$event_list = $Db->getAll("SELECT * FROM `app_factory_event` WHERE `place` = '{$place}'");
		if(empty($event_list)) return false;

		$event = array();

		$count = count($event_list);
		if($count == 1) {
			$event = $event_list[0];
		} else {
			$rand = rand(1 , $count);
			$event = $event_list[$rand - 1];
		}

		// 判断事件的触发概率
		$probability_rand = rand(1 , 100);
		if($probability_rand > $event['probability']) return false;

		// 解析数据
		$event = self::formatData($event);

		return $event;
	}


	/**
	 * 获取一个事件 (根据id获取)
	 * @param <int> $id
	 */
	static public function getById($id) {
		$Db = loadDB();
		$event = $Db->getOne("SELECT * FROM `app_factory_event` WHERE `id` = {$id}");
		if(empty($event)) return false;

		// 解析数据
		$event = self::formatData($event);

		return $event;
	}

	/**
	 * 奖罚
	 * @param <int> $uin
	 * @param <array> $result
	 */
	static public function gain($uin , $result) {
		if(empty($result['gain_type'])) return 0;
		$num = $result['gain_num'];

		switch($result['gain_type']) {
			case 'currency':
				loadLib('Currency');
				return Currency::update('app_factory' , $uin , $num);
			break;
			
			case 'money':
				loadLib('Money');
				return Money::update($uin , $num);
			break;

			case 'hp':
				loadLib('App_factory');
				return App_factory::updateHP($uin , $num);
			break;

			case 'item':
				// todo...
			break;
		}

		return 0;
	}


	/**
	 * 事件数据解析
	 * @param <array> $event
	 */
	static public function formatData($event) {
		$event['guide'] = unserialize($event['guide']);
		$event['info'] = unserialize($event['info']);
		$event['result'] = unserialize($event['result']);
		$event['place_name'] = self::getPlaceName($event['place']);

		return $event;
	}


	/**
	 * 更新事件发生时间和id
	 * @param <int> $uin
	 * @param <int> $id
	 */
	static public function updateTimer($uin , $id) {
		$update_array = array(
			'event_id' => $id,
			'event_timer' => isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(),
		);


		$Db = loadDB();
		$success = $Db->doUpdate('app_factory_info' , $update_array , '`uin` = ' . $uin);

		return $success ? 0 : 1210;
	}


	/**
	 * 事件内容格式化，支持ubb
	 * @param <string> $str
	 */
	static public function formatContent($str) {
		$str = htmlspecialchars($str);
		$str = Format::ubb($str);
		$str = str_replace("\n" , '<br />' , $str);

		return $str;
	}



	/**
	 * 获取地点对应的中文名
	 * @param <string> $place 地点
	 */
	static public function getPlaceName($place = null) {
		$places = array(
			'office_boss' => '老板办公室',
			'office_secretary' => '秘书办公室',
			'office_police' => '保安办公室',
			'dept_admin' => '行政部',
			'dept_construction' => '建设部',
			'dept_hr' => '人事部',
			'dept_market' => '市场部',
			'gongshangju' => '工商局',
			'pk' => 'PK台',
		);

		if(empty($place)) return $places;
		return isset($places[$place]) ? $places[$place] : '';
	}
}


# end of script

